<?php
require_once 'vendor/autoload.php';
use library\DaemonCore;
const ROOT_PATH = __DIR__;
const PID_FILE_PATH = __DIR__.DIRECTORY_SEPARATOR."runtime".DIRECTORY_SEPARATOR."pidfile".DIRECTORY_SEPARATOR."pid";
const MAX_PTHREAD = 50;
//$cfg = \Noodlehaus\Config::load(ROOT_PATH.DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR."config.xml");
$core = new DaemonCore();
DaemonCore::$_logger = \library\Monolog::getLogger('system');
DaemonCore::$_appName = "panda";
if(DaemonCore::isRuning())die("请勿重复启动");
DaemonCore::start();
//新建队列连接
QueueTools::init();
$currentTask = 0;
while (! DaemonCore::$_isDying){
     //检查当前有多少个子进程在启动
     $countPthread = DaemonCore::getSonPthreadCount();
     if($countPthread > MAX_PTHREAD){
         DaemonCore::$_logger->info("当前子进程已满,等待...");
         //延迟消费
         DaemonCore::iterate(2);
         continue;
     }
    //获取队列里的消息
    $payload = QueueTools::getPayload();
    $pid = pcntl_fork();
    if ($pid > 0) {
        //父进程走这里
        DaemonCore::addSonPthread(posix_getpid());
    } else if (0 == $pid) {
        //增加子进程记录
        //运行具体业务处理逻辑
        exit; // 一定要退出，否则子进程会继承上下文继续执行
    } else {
        echo "fork失败" . PHP_EOL;
    }
    DaemonCore::iterate();
}
DaemonCore::stop();
